package comm;

import net.ClientSession;
import game.Game;
import game.Player;
import game.Room.Direction;

public class Commands {
   
   private ClientSession clientSess;
   private Player p;
   private Game game;
   private String n = System.getProperty("line.separator");
   private String[] command = new String[] { "HIGHSCORE", "SAY", "NAME", "LOOK", "END", "HOME", "NORTH", "EAST", "SOUTH", "WEST" };
   
   public Commands(ClientSession clientSess, Game game) {
      this.clientSess = clientSess;
      this.game = game;
   }
   
   /**
    * Checks and excecutes a command.<br>
    * Also executes long commands such as "NORTH NORTH NORTH LOOK SAY HELLO".<br>
    * @param in     incoming string (first word has to be a command, other can)
    * @throws IllegalCommandException   if the command is not registered in the command array
    */
   public void execute(String in) throws IllegalCommandException {
      String[] words = in.split(" ");
      String cmd = words[0];
      
      boolean isValid = false;
      for (String c : command) {
         if (cmd.equalsIgnoreCase(c)) {
            isValid = true;
            break;
         }
      }
      if (!isValid) throw new IllegalCommandException(cmd);
      
      String msg = null;
      StringBuffer strbfr = new StringBuffer();
      
      if (words.length > 1) {
         int i = 1;
         for (;i < words.length - 1; i++) {
            strbfr.append(words[i] + " ");
         }
         strbfr.append(words[i]);
      }
      
      msg = strbfr.toString();
      
      if (cmd.equalsIgnoreCase("NAME")) {
         if (p != null) {
            clientSess.dispatch("You cannot change your name mid-game!");
            return;
         }
         if (msg == null || msg.equals("")) {
            clientSess.dispatch("Please enter your name after the command!");
            return;
         }
         p = clientSess.setPlayer(msg);
         clientSess.dispatch("Welcome, " + msg);
         clientSess.getCommunications().dispatchServerMsg(msg + " HAS ENTERED!");
         clientSess.say("I have joined!");
         return;
      }
      
      if (p == null) {
         clientSess.dispatch("Please enter your name first! ('NAME Your Name')");
         return;
      }
      
      if (cmd.equalsIgnoreCase("NORTH") || cmd.equalsIgnoreCase("EAST") || 
            cmd.equalsIgnoreCase("SOUTH") || cmd.equalsIgnoreCase("WEST")) {
         
         if (cmd.equalsIgnoreCase("NORTH")) {
            if (p.move(Direction.NORTH) == null) {
               clientSess.dispatch("This room doesn't have a NORTH gate!");
               return;
            }
         } else if (cmd.equalsIgnoreCase("EAST")) {
            if (p.move(Direction.EAST) == null) {
               clientSess.dispatch("This room doesn't have a EAST gate!");
               return;
            }
         } else if (cmd.equalsIgnoreCase("SOUTH")) {
            if (p.move(Direction.SOUTH) == null) {
               clientSess.dispatch("This room doesn't have a SOUTH gate!");
               return;
            }
         } else if (cmd.equalsIgnoreCase("WEST")) {
            if (p.move(Direction.WEST) == null) {
               clientSess.dispatch("This room doesn't have a WEST gate!");
               return;
            }
         }
         if (p.getRoom().isExit() == true) {
            game.addScore(p);
            clientSess.dispatch("Congratulations! You made it out alive with " + p.getCount() + " turns."
                  + n + "- HIGHSCORES -" + n + game.getHighScoreAsString());
            clientSess.close();
            return;
         }
         int turnsLeft = p.getTurns() - p.getCount();
         if (turnsLeft < 0) {
            p.getRoom().removePlayer(p);
            p.getRoom().addBody(p.toString());;
            clientSess.dispatch("Cannot.. resist.. need.. sleep.." + n + "YOU HAVE DIED!");
            clientSess.close();
            return;
         }
         clientSess.dispatch("You moved to room " + p.getRoom() + " (" + turnsLeft + " turns left)");
      }
      
      if (cmd.equalsIgnoreCase("LOOK")) {
         clientSess.dispatch(p.look());
      } else if (cmd.equalsIgnoreCase("SAY")) {
         clientSess.say(msg);
         return;
      } else if (cmd.equalsIgnoreCase("HIGHSCORE")) {
         clientSess.dispatch("- HIGHSCORES -" + n + game.getHighScoreAsString());
      } else if (cmd.equalsIgnoreCase("END")) {
         p.getRoom().removePlayer(p);
         clientSess.dispatch("Bye!");
         clientSess.close();
         return;
      } else if (cmd.equalsIgnoreCase("HOME")) {
         p.setBackToHome();
         clientSess.dispatch("Back home! (Room " + p.getRoom() + ")");
      }
         
      if (msg != null && !msg.equals(""))execute(msg);
   }
}
